package cn.walls1717.ggkt.vod.service.impl;

import cn.walls1717.ggkt.model.vod.Video;
import cn.walls1717.ggkt.vod.mapper.VideoMapper;
import cn.walls1717.ggkt.vod.service.VideoService;
import cn.walls1717.ggkt.vod.service.VodService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 课程视频服务实现类
 *
 * @author walls1717
 * @since 2022/07/14
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Autowired
    private VodService vodService;

    /**
     * 根据课程id删除
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeByCourseId(Long id) {
        LambdaQueryWrapper<Video> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Video::getCourseId, id);
        // 根据课程id查询到对应的小节
        List<Video> videoList = this.list(lqw);
        // 判断小节是否有视频，有视频先删除视频
        for (Video video : videoList) {
            String videoSourceId = video.getVideoSourceId();
            if (videoSourceId != null) {
                vodService.deleteVideo(videoSourceId);
            }
        }
        // 根据课程id删除小节
        this.remove(lqw);
    }

    /**
     * 根据videoId删除
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteVideoById(Long id) {
        // 根据videoId获取到video对象
        Video video = this.getById(id);
        String videoSourceId = video.getVideoSourceId();
        // 判断是否有视频，如果有先删除视频
        if (videoSourceId != null) {
            vodService.deleteVideo(videoSourceId);
        }
        // 删除video信息
        this.removeById(id);
    }
}
